<!-- 项目合作标准 -->
<template>
    <div>
        <top-bar 
        :form='searchForm'
        @searchList="searchList"
        @add="viewList('add')"
        @push="pushAll"
        ></top-bar>
        <table-list 
        :tableData='tableData'
        :tableInfos ='tableInfos'
        :tableChangeBtns='tableChangeBtns'
        :tableFormatter='tableFormatter'
        :selectable='selectable'
        :hasSera='true'
        :activeBtn='tableBtnRule'
        @selectChange="selectChange"
        >
        </table-list>
        <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            style="margin-top:30px"
            :current-page.sync="pageNum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :total="totalSize">
        </el-pagination>
    </div>
</template>
<script>
import api from '@/api/apiPath'
import tableList from '../common/tableList'
import topBar from '../common/topChangeBar'
export default {
    title:'项目合作标准',
	components: {
        'table-list':tableList,
        'top-bar':topBar
        },
	data () {
		return {
            seleArray:[],
            searchForm:{
                searchName:'',
                examStatus:'',
                status:'',
            },
            tableInfos:[
                {label:'标准名称',prop:'name'},
                {label:'业务线',prop:'lineBusiness'},
                {label:'项目类型',prop:'lineType'},
                {label:'合作模式',prop:'lineCollaborat'},
                {label:'有无预售证',prop:'inPresale'},
                {label:'垫佣开发商要求',prop:'inPadRanking'},
                {label:'垫佣周期',prop:'inPadExpiryEqualAll'},
                {label:'是否收电商',prop:'inEcommerce'},
                {label:'电商政策',prop:'inEcommercePolicy'},
                {label:'开发商佣金占比',prop:'proporBusinessmanEqualAll'},
                {label:'好生活平台费占比',prop:'proporEasylifeEqualAll'},
                {label:'测算净利润占比',prop:'proporNetprofitEqualAll'},
                {label:'版本号',prop:'inNumber'},
                {label:'更新人',prop:'updateName'},
                {label:'更新时间',prop:'updateTime'},
                {label:'审核状态',prop:'examineStatus'},
                {label:'状态',prop:'inApplication'},
                {label:'操作',prop:'changeBtn'},
            ],
            tableChangeBtns:[
                {label:"发起审批",type:'push',fn:this.push},
                {label:"启用",type:'start',fn:this.start},
                {label:"撤回",type:'recall',fn:this.recall},
                {label:"编辑",type:'edit',fn:this.viewList},
                {label:"详情",type:'view',fn:this.viewList},
                {label:"删除",type:'dele',fn:this.deleteItem}
                ],
            tableData:[],
            pageNum: 1,
            pageSize: 10,//每页几条
            totalSize: 0,
        }
    },
    methods:{
        //表格格式化方法。prop对应array下标
        tableFormatter(prop,cellValue){
            let valueMap = {
                lineBusiness:['新房'],
                lineType:['外部项目','内部项目'],
                lineCollaborat:['包销模式','分销模式','合伙人模式','加盟模式','保证金模式'],
                inPresale:['无','有'],
                inPadRanking:['无','排名前100'],
                inEcommerce:['否','是'],
                inEcommercePolicy:['无','允许','不允许'],
                examineStatus:['未审批','审批中','审批通过','审批不通过'],
                inApplication:['未启用','已启用'],
            }
            return valueMap[prop]?valueMap[prop][cellValue]:cellValue;
        },
        //表格多选校验
        selectable(row,idx){
            let {examineStatus} = this.tableData[idx];
            if(examineStatus == 0 || examineStatus == 3) return true;
            return false;
        },
         //按钮显示规则
        tableBtnRule(rowData,btns){
            let [...newbtn] = btns;
            //0发起审批，1启用，2撤回，3编辑，4详情，5删除
            
            newbtn.forEach(btn => {
                btn.disabled = true;
            });
            //详情一直可用
            newbtn[4].disabled =false;
            //未启用并且未审核或者审核不通过
            if(rowData.inApplication == 0 && (rowData.examineStatus == 0 || rowData.examineStatus == 3) ){
                newbtn[0].disabled =false;
                newbtn[5].disabled =false;
            }
            //未启用并且不是审核中
            if(rowData.inApplication == 0 && rowData.examineStatus != 1){
                 newbtn[3].disabled =false;
            }
            //未启用并且审核通过
            if(rowData.inApplication == 0 && rowData.examineStatus == 2){
                 newbtn[1].disabled =false;
            }
            //已启用并且审核通过
            if(rowData.inApplication == 1 && rowData.examineStatus == 2){
                 newbtn[2].disabled =false;
            }
            if(rowData.inApplication == 1) {
                newbtn.splice(1,1);
            }else{
               newbtn.splice(2,1);
            }
            
            return newbtn;
        },
        selectChange(selects){
            this.seleArray = selects;
        },
        handleClick(){},
        //新增查看编辑
        viewList(type,idx){
            let insideId;
            if(type != 'add')  insideId= this.tableData[idx].id;
            this.$router.push({path: '/changeCooperate', query: {pageType:type,insideId:insideId}})
            
            },
        //删除
        deleteItem(type,idx){
            this.$confirm(`确定要删除当前列表吗？`, '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
                let params={
                   id:this.tableData[idx].id,
                   indelect:1,
                }
                this.axios.post(api.deleProjectcooprtate, params).then(res => {
                    if (res.data.code == 200) {
                        this.tableData.splice(idx,1);
                        this.$message({
                            type: 'success',
                            message: '已删除',
                        });
                        this.searchList();
                    }
                })
                
            }).catch(() => {
                
            });
        },
        searchList(pageNum){
            //搜索或者切换每页数量重置页码为1
            let params = {
              pageNum:pageNum||this.pageNum,
              pageSize:this.pageSize,
              name:this.searchForm.searchName,
              examineStatus:this.searchForm.examStatus,
              inApplication:this.searchForm.status
            };
            this.axios.post(api.getProjectcooprtateList, params).then(res => {
                if (res.data.code == 200) {
                    let list = res.data.data.list || [];
                    list.forEach(data=>{
                        let equalArray = ["=","≠",">","≥","<","≤"];
                        let {
                            inPadExpiry,
                            inPadExpiryEqual,
                            proporBusinessman,
                            proporBusinessmanEqual,
                            proporEasylife,
                            proporEasylifeEqual,
                            proporNetprofit,
                            proporNetprofitEqual
                            } = data;
                        data.inPadExpiryEqualAll=inPadExpiry?(`${equalArray[inPadExpiryEqual]}${inPadExpiry}个月`):"无"
                        data.proporBusinessmanEqualAll=`${equalArray[proporBusinessmanEqual]}${proporBusinessman}%`;
                        data.proporEasylifeEqualAll=`${equalArray[proporEasylifeEqual]}${proporEasylife}%`;
                        data.proporNetprofitEqualAll=`${equalArray[proporNetprofitEqual]}${proporNetprofit}%`;
                      
                    })
                    this.tableData = list;
                    this.totalSize = res.data.data.total;
                }
            })
            
        },
        //启用
        start(type,idx){
            this.$confirm(`确定要启用吗？`, '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
                let params={
                    id:this.tableData[idx].id,
                    inApplication:1,
                }
                this.axios.post(api.changeProjectcooprtate, params).then(res => {
                    if (res.data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '已启用',
                        });
                        this.searchList();
                    }
                })
                
            }).catch(() => {
                
            });
        },
        //撤回
        recall(type,idx){
            this.$confirm(`确定要撤回吗？`, '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
               let params={
                    id:this.tableData[idx].id,
                    inApplication:0,
                }
                this.axios.post(api.changeProjectcooprtate, params).then(res => {
                    if (res.data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '已撤回',
                        });
                        this.searchList();
                    }
                })
                
            }).catch(() => {
                
            });
        },
        //单条审批
        push(type,idx){
            this.$confirm(`确定要发起审批吗？`, '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
                 let id = this.tableData[idx].id;
                 let params={
                    idList:[id],
                    type:1
                }
                this.axios.post(api.apovalFstandardLists, params).then(res => {
                    if (res.data.code == 200) {
                        this.$message({
                        type: 'success',
                        message: '已发起审批',
                        });
                        this.searchList();
                    }
                })
                
            }).catch(() => {
                
            });
        },
        //批量发起审批
        pushAll(){
            let listLength = this.seleArray.length;
            if(!listLength) {
                this.$alert("请选择提交审批的数据", '提示', {
                    type: 'error',
                    confirmButtonText: '确定',
                });
                return ;
            }
            let comfirmText = `一共选择了${listLength}条可提交审批的数据，确定要发起审批吗？`
           this.$confirm(comfirmText, '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
                let controlIds = [];
                this.seleArray.forEach(sele=>{
                    controlIds.push(sele.id)
                })
                let params={
                    idList:controlIds,
                    type:1
                }
                this.axios.post(api.apovalFstandardLists, params).then(res => {
                    if (res.data.code == 200) {
                        this.$message({
                        type: 'success',
                        message: '已发起审批',
                        });
                        this.searchList();
                    }
                })
            }).catch(() => {
                
            });
        },
        handleCurrentChange () {
            this.searchList()
		},
		handleSizeChange (val) {
			this.pageSize = val
			this.searchList(1)
		},
	},
	created () {
        this.searchList();
    },
    activated(){
        this.searchList();
    },
	destroyed() {
		
	},
	filters: {

	}
}

</script>

<style lang="stylus" scoped>


</style>
